/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ourgame.tankgame.control;

import com.ourgame.tankgame.control.tank.TankControl;
import com.jme3.asset.AssetManager;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.Matrix3f;
import com.jme3.renderer.Camera;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import gameutil.Globals;

/**
 *
 * @author echoff
 */
public class BaseControlSystem extends ControlSystem {

    public BaseControlSystem() {
    }

    public BaseControlSystem(InputManager inputManager) {
        this.setInputManager(inputManager);
    }

    public BaseControlSystem(InputManager inputManager, Camera cam) {
        this(inputManager);
        this.setCam(cam);
    }
    
    public BaseControlSystem(AssetManager assetManager, InputManager inputManager, Camera cam) {
        this(inputManager, cam);
        this.setAssetManager(assetManager);
    }

    @Override
    public void onAction(String binding, boolean isPressed, float tpf) {
//        super.onAction(name, isPressed, tpf);
        if (binding.equals("Lefts")) {
            moveSystem.turnLeft(isPressed);
        } else if (binding.equals("Rights")) {
            moveSystem.turnRight(isPressed);
        } else if (binding.equals("Ups")) {
            moveSystem.moveAhead(isPressed);
        } else if (binding.equals("Downs")) {
            moveSystem.moveBack(isPressed);
        } else if (binding.equals("Shoot") && isPressed) {
            weaponSystem.fire();
        } else if (binding.equals("Reset")) {
            if (isPressed) {
                System.out.println("Reset");
                /*
                 * 这里有点问题
                 */
                spatial.getControl(TankControl.class).setPhysicsLocation(Globals.INIT_POS);
                spatial.getControl(TankControl.class).setPhysicsRotation(new Matrix3f());
                spatial.getControl(TankControl.class).clearForces();
            } else {
            }
        }
    }

    @Override
    public void onAnalog(String name, float value, float tpf) {
//        super.onAnalog(name, value, tpf);
    }

    @Override
    protected void controlUpdate(float tpf) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void initControlSystem() {
        setupKeys(getInputManager());
    }

    private void setupKeys(InputManager inputManager) {
        inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping("Shoot", new KeyTrigger(KeyInput.KEY_SPACE));
        inputManager.addMapping("Reset", new KeyTrigger(KeyInput.KEY_RETURN));


        inputManager.addListener(this, "Lefts");
        inputManager.addListener(this, "Rights");
        inputManager.addListener(this, "Ups");
        inputManager.addListener(this, "Downs");
        inputManager.addListener(this, "Shoot");
        inputManager.addListener(this, "Reset");

    }
}
